package cn.zyb.org.controller;

import cn.zyb.basic.util.AjaxResult;
import cn.zyb.basic.util.PageList;
import cn.zyb.org.domain.Employee;
import cn.zyb.org.query.EmployeeQuery;
import cn.zyb.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @author zyb
 * @version 1.0.0
 * @date 2022年07月03日 19:09:39
 * @packageName cn.zyb.org.controller
 * @className EmployeeMapper
 * @describe TODO
 */
/**
 * 接口类：员工
 */
@RestController
@RequestMapping("/employee")
@Api(value = "员工接口类",description = "员工接口的CRUD操作")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 接口：查询所有
     * @return
     */
    // http://localhost:8080/employee - Get
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 接口：查询单个对象
     * @return
     */
    // http://localhost:8080/employee/2 - Get
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public Employee findOne(@PathVariable("id") Long id){

      return employeeService.findOne(id);
    }

    /**
     * 分页接口
     * @param employeeQuery
     * @return
     */
    // http://localhost:8080/employee - Post - 参数通过json对象传递
    @PostMapping
    //@RequestBody -从请求体中获取数据赋值给employeeQuery
    @ApiOperation(value = "分页接口")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    // http://localhost:8080/employee/2 - Delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
//            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    /**
     * 接口：添加和修改
      * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){//添加
                employeeService.add(employee);
            }else{
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

  /**
   * 接口：批量删除
   * @param ids
   * @return
   */
  @PatchMapping
  @ApiOperation(value = "批量删除接口")
  public AjaxResult patchDel(@RequestBody List<Long> ids){
    try {
      employeeService.patchDel(ids);
      return new AjaxResult();
    } catch (Exception e) {
      e.printStackTrace();
      return new AjaxResult(false,"删除失败");
    }
  }
}
